import Vue from 'vue'
import Vuex from 'vuex'
import { login } from '@/api/user'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || '',
    refreshToken: localStorage.getItem('refreshToken') || ''
  },
  getters: {
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
    setRefreshToken (state, refreshToken) {
      state.refreshToken = refreshToken
      localStorage.setItem('refreshToken', refreshToken)
    }
  },
  actions: {
    async  login (ctx, value) {
      try {
        const res = await login(value)
        const { token } = res.data.data
        ctx.commit('setToken', token)
        ctx.commit('setRefreshToken', res.data.data.refresh_token)

        return Promise.resolve(res)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  },
  modules: {
  }
})
